<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
model('jy_weishop/member')->checklogin();
$store_id=$this->session('store_id');
if($_RQ['op']=='main'){
	$shopset = plugin('jy_weishop')->plugin_setting('shopset');
	//营销设置
	$saleset=plugin('jy_weishop')->plugin_setting('saleset');
	$goodsid = intval($_RQ['goodsid']);
	$return['setting']=$this->plugin_setting('basic');
	$return['sendtypes']=array();
	if($return['pluginset']['down_send_type1']!=1){
		$return['sendtypes'][1]='快递配送';
		$sendtypeone=1;
	}
	if($return['pluginset']['down_send_type2']!=1){
		$return['sendtypes'][2]='店内自提';
		$sendtypeone=2;
	}
	if($return['pluginset']['down_send_type3']!=1){
		$return['sendtypes'][3]='预约下单';
		$sendtypeone=3;
	}
	$tableid=$this->session('tableid');
	//下单页
	$item=array();
	//提交数据
	$return['post'] = array(
		'sendtype'=>$this->session('sendtype')?:1,
		'self_type'=>$this->session('self_type')?:1,
		'make_type'=>$this->session('make_type')?:0,
		'out_time'=>$this->session('out_time')?:'',
		'dayid'=>$this->session('dayid')?:0,
	);
	if(count($return['sendtypes'])==1){
		$return['post']['sendtype']=$sendtypeone;
		$return['sendtypeone']=$sendtypeone;
	}
	if($tableid){
		$return['sendtypes']=array();
		$return['post']['sendtype']=2;
		$return['table']=pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tableid));
	}
	$return['selectstore']=array();
	if($goodsid){
		$_SESSION['goodsid']=$goodsid;
		//直接购买。
		$return['post']['goodsid'] = $goodsid;
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid));
		if(!$goods){
			exi('指定商品不存在！','error','goback');
		}
		//配送设置
		$goodsdispatch=pdo_get('jy_weishop_goods_dispatch',array('goodsid'=>$goods['id']));
		$goods['fullnum']=$goodsdispatch['fullnum'];
		$goods['fullmoney']=$goodsdispatch['fullmoney'];
		$goods['fullnoareas']=$goodsdispatch['fullnoareas'];
		$goods['template_id']=$goodsdispatch['template_id'];
		$goods['weight']=$goodsdispatch['weight'];
		$goods['dispatchprice']=$goodsdispatch['price'];
		//如果来自其他商户分销（修改商户ID）
		$frommerchid=$this->session('frommerchid');
		$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
		model('jy_weishop/goods')->check_buy($goods['id'],$_RQ['num']);
		//判断距离
		if($goods['merchid']){
			$merset=pdo_getcolumn('core_account_plugin_setting',array('uniacid'=>$_SESSION['uniacid'],'key'=>'peisong'.$goods['merchid'],'plugin'=>'jy_merch'),'data');
			$juli=json_decode($merset,true);
			$juli=$juli['juli'];
			$return['juli']=$juli;
		
		}
		//购买权限：
		$levelinfo = model('jy_weishop/member')->get_level();
		if($goods['buylevels'] && $goods['buylevels'] !=$levelinfo['id']){
			exi('您没有权限购买该商品！','error','index/');
		}
		$goods['goodsid']=$goods['id'];
		$optionsid = intval($_RQ['optionsid']);
		$goods['storeid']=$store_id;
		$goods['optionid']=$optionsid;
		$goods=model('jy_weishop/activity')->get_upgoods($goods);
		//重置下单数量
		$goods['goodstotal']=$goods['total'];
		$goods['total']=intval($_RQ['num']);
		$optionsid = intval($_RQ['optionsid']);
		if($optionsid){
			$options = pdo_get('jy_weishop_goods_option',array('goodsid'=>$goodsid,'id'=>$optionsid));
			$goods['optionid']=$options['id'];
			$goods['price']=$options['price'];
			$return['post']['optionid'] = $optionsid;
		}else{
			$goods['optionid']=0;
			$return['post']['optionid'] = 0;
		}
		$goods=model('jy_weishop/activity')->get_uporder($goods);
		$return['post']['total'] = intval($_RQ['num']);
		$return['post']['invoice'] = $goods['invoice'];
		if($goods['type']==1 || $goods['type']==2){
			//虚拟商品
			//$order['sendtype']=3;
		}elseif($goods['type']==3){
			//计时计次服务，发货类型必须是核销
			//$order['sendtype']=2;
		}elseif($goods['type']==0 || $goods['isverify']){
			//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
			$carrier_list = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
			if(!$carrier_list){
				$order['sendtype']=1;
			}else{
				foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
				}
				$return['carrier_list']=$carrier_list;
				$return['selectstore']=$carrier_list['0'];
			}
		}
		$order=array(
			'sendtype'=>$order['sendtype']?:$return['post']['sendtype'],
			'type'=>'one',
			'list'=>array($goods),
		);
	}else{
		//从购物车购买
		//$order['sendtype']=1;//点餐除外
		$cart_list=pdo_getall('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1),array('id','goodsid','total','optionid','merchid'));
		if(!$cart_list){
			exi('您的购物车没有商品！','error','goodslist/');
		}
		foreach($cart_list as &$cart){
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),array('id','title','image','price','total','totalcnf','weight','active_plugin','active_id','storeids','merchid','type','isverify'));
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			//配送设置
			$goodsdispatch=pdo_get('jy_weishop_goods_dispatch',array('goodsid'=>$cart['goodsid']));
			$goods['fullnum']=$goodsdispatch['fullnum'];
			$goods['fullmoney']=$goodsdispatch['fullmoney'];
			$goods['fullnoareas']=$goodsdispatch['fullnoareas'];
			$goods['template_id']=$goodsdispatch['template_id'];
			$goods['weight']=$goodsdispatch['weight'];
			$goods['dispatchprice']=$goodsdispatch['price'];
			model('jy_weishop/goods')->check_buy($cart['goodsid'],$cart['total'],$cart['optionid']);
			$goods['storeid']=$store_id;
			$goods['optionid']=$cart['optionid'];
			$goods=model('jy_weishop/activity')->get_upgoods($goods);
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			if($cart['optionid']){
				if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
				}
				$goods['price']=$goods['options']['price'];
				$specs = explode("_", $goods['options']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
				}
				$goods['optiondata']= $optiontitle;
			}
			//来自商户的分销
			$cart['merchid']=$cart['merchid']?$cart['merchid']:$goods['merchid'];
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				//$order['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是核销
				//$order['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				$carrier_list = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
				if(!$carrier_list){
					$order['sendtype']=1;
				}else{
					foreach($carrier_list as &$pv){
						$pv['pcd']=json_decode($pv['pcd'],true);
					}
					$return['carrier_list']=$carrier_list;
					$return['selectstore']=$carrier_list['0'];
				}
			}
			$cart=array_merge($goods,$cart);
			
		}
		$order=array(
			'sendtype'=>$order['sendtype']?:$return['post']['sendtype'],
			'type'=>'cart',
			'list'=>$cart_list,
		);
	}
	if($store_id){
		$return['selectstore']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	}
	if($return['selectstore']['id']){
		$return['selectstore']['pcd']=json_decode($return['selectstore']['pcd'],true);
		$return['selectstore']['juli']=1;
		$return['selectstore']['out_juli']=$return['selectstore']['out_juli']>0?$return['selectstore']['out_juli']:0;
	}
	if(empty($order['list'])){
		exi('请先选择商品再下单！','error','goback');
	}
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	$total_weight=0;
	foreach($order['list'] as &$vc){
		$total+=$vc['total'];
		$total_price+=$vc['total']*$vc['price'];
		$total_weight+=$vc['weight']*$vc['total'];
	}
	
	$order['total']=$total;
	$order['total_price']=$total_price;
	$order['total_weight']=round(($total_weight/1000)*1000,2);
	//计算运费
	$item['address']=array('address'=>'');
	$order['dispatchprice']=0;
	if(!$order['sendtype'] || $order['sendtype']==1 || $return['sendtypes']){
		if(intval($_RQ['addressid'])){
			$item['address']=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'id'=>intval($_RQ['addressid'])));
			$item['addressid']=intval($_RQ['addressid']);
		}else{
			$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
			if($member_address){
				foreach($member_address as &$address){
					if($address['isdefault']){
						$item['addressid']=$address['id'];
						$item['address']=$address;
						break;
					}
				}
				if(!$item['addressid']){
					$item['addressid']=$member_address['0']['id'];
					$item['address']=$member_address['0'];
					
				}
				
			}
		}
		
		if($item['address']){
			$item['address']['pcd']=json_decode($item['address']['pcd'],true);
			/*try{
				$order['dispatchprice']=model('jy_weishop/order/dispatch')->get_freight($order['list'],$item['address']['pcd'],true);
			}catch(Exception $e){
				unset($item['address'],$item['addressid']);
				$order['dispatchprice']=0;
				$item['isnoarea']=1;
			}*/
			//外卖配送费用固定：
			$order['olddispatchprice']=$return['selectstore']['out_pay']>0?$return['selectstore']['out_pay']:$return['pluginset']['out_fee'];
			if($order['total_price']>=$return['pluginset']['man_fee'] && $return['pluginset']['man_fee']>0){
				$order['dispatchprice']=0;
			}else{
				$order['dispatchprice']=$return['selectstore']['out_pay']>0?$return['selectstore']['out_pay']:$return['pluginset']['out_fee'];
			}
		}
	}
	//会员折扣价
	$order['level_de_price']=0.00;
	if(!$order['no_member_discount']){
		//允许使用会员折扣，整理会员折扣数据
		$member_levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid']),'level');
		if($member_levelid){
			$member_level=pdo_get('jy_weishop_member_level',array('id'=>$member_levelid));
			if($member_level){
				$member_level['discount']=$member_level['discount']?$member_level['discount']:10;
				$order['level_de_price']=$order['total_price']-$order['total_price']*($member_level['discount']/10);
				$order['level_de_price']=round($order['level_de_price'],2);
			}
		}
	}
	//运势折扣价
	$order['luck_de_price']=0.00;
	$sale_luck=plugin('jy_weishop')->plugin_setting('sale_luck');
	$myluckkey=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'luckkey');
	if($myluckkey && $sale_luck['isopen']){
		$discount=$sale_luck['lucklist'][$myluckkey]['discount'];
		$discount=($discount>10&&$discount<=100)?$discount:100;
		$order['luck_de_price']=$order['total_price']-($order['total_price']*$discount/100);
	}
	//满减优惠价格：
	$sale_enough=$saleset['enough'];
	if($sale_enough){
		foreach($sale_enough as $k=>$ev){
			if($ev['man']>$order['total_price']){
				unset($sale_enough[$k]);
			}
		}
		paixu($sale_enough,'man','desc');
	}
	$order['sale_enough']=$sale_enough;
	$order['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
	//优惠券优化价格：
	$order['coupon_de_price']=0.00;
	if(!$order['no_coupon_discount']){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list']);
		$order['coupon_de_price']=$newcoupons[0]['de_price'];
		$item['couponid']=$newcoupons[0]['id'];
		$return['couponcount']=count($newcoupons);
	}
	$return['coupons']=$newcoupons?$newcoupons:array();
	//整理收货地址信息
	$return['address_list']=pdo_getall('core_address',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['address_item']=array();
	$order['coupon']=$newcoupons[0]['coupon'];
	//提交数据
	$return['post']['couponid'] = $newcoupons[0]['id'];
	$return['post']['title5'] = $newcoupons[0]['coupon']['title5'];
	$return['post']['deduct'] = price($newcoupons[0]['coupon']['deduct']);
	$return['post']['discount'] = $newcoupons[0]['coupon']['discount'];
	$return['post']['backtype'] = $newcoupons[0]['coupon']['backtype'];
	$return['post']['addressid'] = $item['addressid'];
	$return['post']['sendtype'] = $order['sendtype'];
	$return['post']['sendtime'] = strtotime("+2 day");
	$return['post']['storeid'] = $return['selectstore']['id'];
	$return['post']['deliverytime'] = date("m月d日",strtotime("+2 day"));
	//配送
	$order['deliverytime'] = $return['post']['deliverytime'];
	//整理最终价(暂不扣除优惠券)
	$order['endprice']=round($order['total_price'] + $order['dispatchprice'] - $order['level_de_price'] - $order['luck_de_price'] - $order['enough_de_price'],2);
	$order['no_dispatch_price']=round($order['total_price'] - $order['level_de_price'] - $order['luck_de_price'] - $order['enough_de_price'],2);	
	//开启了加急模式
	$return['post']['endprice']=$order['endprice']- $order['coupon_de_price'];
	$return['post']['endprice2'] = $return['post']['endprice'] + intval($this->setting['out_feejia']);
	$return['post']['no_dispatch_price']=$order['no_dispatch_price']- $order['coupon_de_price'];
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			if($gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			if($gift['goodsid']){
				foreach($order['goods'] as $goods){
					if($goods['goodsid']==$gift['goodsid']){
						$current_gift=$gift;
						break;
					}
				}
			}
			if($current_gift){
				break;
			}
		}
	}
	if($current_gift){
		$order['gift_id']=$current_gift['id'];
		$return['gifts']=json_decode($current_gift['gifts'],true);
		foreach($return['gifts'] as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id']),'price');
		}
	}
	$order['total_price']=round($order['total_price'],2);
	$_SESSION['order']=$return['order']=$order;
	$item['sendtype']=$order['sendtype'];
	$return['item']=$item;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	//自提点
	
	set_html('title','确认订单');
	//代付
	$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
	if($peerpay_setting && $peerpay_setting['open'] && $order['endprice']>0){
		$return['peerpay']=true;
	}
	exi($return);
}

if($_RQ['op']=='juli'){
	//判断距离
		$post=get_postdata();
		$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
		$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
		if($l['lng'] && $l['lat']){
			$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
			if($d/1000>$post['juli']){
				exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
				exi('success');
			}else{
				exi('success');
			}
		}	
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//整理订单数据
	$order=$_SESSION['order'];
	//公共参数
	if($post['out_time']){
		//配送时间
		$days = array(
				0=>date("Y-m-d",time("day")),
				1=>date("Y-m-d",strtotime("+1 day")),
				2=>date("Y-m-d",strtotime("+2 day")), 
				3=>date("Y-m-d",strtotime("+3 day")), 
				4=>date("Y-m-d",strtotime("+4 day")),
				5=>date("Y-m-d",strtotime("+5 day")),
				6=>date("Y-m-d",strtotime("+6 day")),
		);
		$dayid=intval($post['dayid']);
		$post['out_time']=$days[$dayid].' '.$post['out_time'];
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'goodsprice'=>$order['total_price'],
		'remark'=>trim($post['remark']),
		'goods'=>$order['list'],
		'merchid'=>$order['merchid'],
		'storeid'=>intval($post['storeid'])?:$store_id,
		'self_type'=>intval($post['self_type']),
		'out_time'=>trim($post['out_time']),
		'make_type'=>intval($post['make_type']),
	);
	$new_order['invoce']=array(
		'type'=>$post['invoicetype'],
		'goodstype'=>$post['invoicegoodstype'],
		'name'=>$post['invoicename'],
		'code'=>$post['invoicecode'],
		'email'=>$post['email'],
	);
	//会员折扣
	if(!$order['no_member_discount'] && $order['level_de_price']){
		$new_order['level_de_price']=$order['level_de_price'];
	}
	//运势折扣
	if($order['luck_de_price']){
		$new_order['luck_de_price']=$order['luck_de_price'];
	}
	//满减
	$new_order['enough_de_price']=$order['enough_de_price'];
	if($post['sendtype']){
		$new_order['sendtype']=intval($post['sendtype']);
	}else{
		$sendtype=$order['sendtype'];
		if(!$sendtype){
			exi('请先选择就餐方式！','error');
		}
		$new_order['sendtype']=$sendtype;
	}
	$new_order['price']=$new_order['goodsprice']-intval($new_order['enough_de_price'])-$new_order['level_de_price']-$new_order['luck_de_price'];
	//优惠券
	if(!$order['no_coupon_discount']){
		$couponid=intval($post['couponid']);
		//获取优惠信息
		$getcouponfee=model('jy_weishop/coupon')->getorderfee($couponid,$new_order['price']);
		if($getcouponfee){
			$new_order=array_merge($new_order,$getcouponfee);
		}
	}
	if($new_order['sendtype']==1){
		//快递配送
		$new_order['addressid']=intval($post['addressid']);
		if(!$new_order['addressid']){
			exi('请先选择收货地址！','error');
		}
		$pcd=pdo_getcolumn('core_address',array('id'=>$new_order['addressid']),'pcd');
		if($pcd){
			$pcd=json_decode($pcd,true);
			$new_order['dispatchprice']=$order['dispatchprice'];
			//开启了加急模式
			if($new_order['make_type']==0 && $this->setting['on_feejia']){
				$new_order['dispatchprice'] += intval($this->setting['out_feejia']);
			}
			$new_order['price']+=$new_order['dispatchprice'];
		}		
		
		if($post['juli']){
		$merchid=pdo_getcolumn('jy_weishop_goods',array('id'=>$_SESSION['goodsid']),'merchid');
		$l=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('lat','lng'));
		if($l['lng'] && $l['lat']){
			$d=cfc('tools')->getdistance($post['lat_lng']['lng'],$post['lat_lng']['lat'],$l['lng'],$l['lat']);
			if($d/1000>$post['juli']){
				exi('超出配送距离,无法购买','error','/goods.detail//id='.$_SESSION['goodsid']);
			}
		}
		}		
	}elseif($new_order['sendtype']==2){
		//门店自提
		if(!$new_order['storeid']){
			exi('请先选择门店！','error');
		}
	}elseif($new_order['sendtype']==3){
		//虚拟发货
		
	}
	$new_order['price']=$new_order['price']>0?$new_order['price']:0;
	if($new_order['price']<=0){
		exi('订单金额不能为零！','error');
	}
	if($order['gift_id']){
		$new_order['gift_id']=$order['gift_id'];
	}
	$new_order['plugin_name']='jy_weidish_naixue';
	$new_order['isdish']=1;
	$new_order['tableid']=$this->session('tableid');
	$order_id=model('jy_weishop/order')->create($new_order,'',$url);
	unset($_SESSION['order']);
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_weidish_naixue/create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>'jy_weidish_naixue/orderdetail//id='.$order_id,
		'error_url'=>'jy_weidish_naixue/orderdetail//id='.$order_id,
	);
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
	if($post['peerpay']){
		$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open']){
			exi('','','/order.peerpay//orderid='.$order_id);
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	exi($res);
}
if($_RQ['op']=='get_freight'){
	$addressid=intval($_RQ['address']);
	$addressid=$addressid?:$_RQ['address']['id'];
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	$pcd=pdo_getcolumn('core_address',array('id'=>$new_order['addressid']),'pcd');
	if(!$pcd){
		exi('0');
	}
	$pcd=json_decode($pcd,true);
	$freight=model('jy_weishop/order/dispatch')->get_freight($_SESSION['order']['list'],$pcd);
	exi($freight);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_weishop/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_weishop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_weishop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	$url=custom_url('jy_weidish_naixue/orderdetail//id='.$order['id'],true);
	model('jy_weishop/order')->order_pay($order['id'],$params,$url);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	//$number = pdo_count('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'createtime >'=>$daytime));
	//$number='NO'.sprintf("%05d", $number+1);
	//pdo_insert('jy_weishop_queue_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'orderid'=>$order['id'],'store_id'=>$store_id,'number'=>$number,'createtime'=>TIMESTAMP));
	if($order['merchid']){
		$mid=pdo_getcolumn('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),'mid');
		$setting=core_setting('cloud');//获取云服务参数
		//生成前端签名
		$sign=sha1(TIMESTAMP.$setting['cloudkey'].$_SESSION['uniacid'].$_SESSION['uid'].$setting['secret']);
		//拼接前端发送消息的前缀
		$return['message_pre1']=$sign.';'.TIMESTAMP.';'.$setting['cloudkey'].';'.$_SESSION['uniacid'].';'.$_SESSION['uid'].';';
		$m=array('status'=>1);
		$message=ijson_encode($m);
		cfc('jinyun_socket')->send2uid($message,$mid);
	}
	
	exi('支付成功!');
}

?>